package com.daniel.cms.controller;

import com.daniel.cms.entity.ArticleEntity;
import com.daniel.cms.service.ArticleService;
import com.daniel.cms.utils.Mes;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 详细说明
 *
 * @author Daniel
 * @version V1.0
 * @date 2020/4/21 10:38
 */
@Api(tags = "文章管理")
@RequestMapping("/article")
@RestController
public class ArticleController {

	@Autowired
	ArticleService service;

	@ApiOperation("获取文章信息")
	@ApiImplicitParams(value = {@ApiImplicitParam(name = "id", dataType = "string", value = "主键", required = true)})
	@GetMapping(value = "/info/{id}")
	public Mes info(@PathVariable String id) {
		return service.info(id);
	}

	@ApiOperation("获取文章列表")
	@ApiImplicitParams(value = {
			@ApiImplicitParam(name = "page", dataType = "int", value = "页索引", required = true, defaultValue = "1"),
			@ApiImplicitParam(name = "limit", dataType = "int", value = "页大小", required = true, defaultValue = "10"),
			@ApiImplicitParam(name = "name", dataType = "string", value = "用户名", defaultValue = "")})
	@GetMapping("/list/{page}/{limit}")
	public Mes list(@PathVariable int page, @PathVariable int limit,
	                @RequestParam(name = "name", defaultValue = "") String name) {
		return service.list(page, limit, name);
	}

	@ApiOperation("新增文章")
	@ApiImplicitParams(value = {@ApiImplicitParam(name = "entity", dataType = "AccountEntity", value = "文章实体")})
	@PostMapping("/create")
	public Mes create(@RequestBody ArticleEntity entity) {
		return service.create(entity);
	}

	@ApiOperation("修改文章")
	@ApiImplicitParams(value = {@ApiImplicitParam(name = "entity", dataType = "AccountEntity", value = "文章实体")})
	@PostMapping("/update")
	public Mes update(@RequestBody ArticleEntity entity) {
		return service.update(entity);
	}

	@ApiOperation("删除文章")
	@ApiImplicitParams(value = {@ApiImplicitParam(name = "entity", dataType = "object", value = "文章编号")})
	@PostMapping(value = "/delete")
	public Mes delete(@RequestBody Map<String, String> map) {
		return service.delete(map);
	}
}
